{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:29: DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release.\n",
      "  from numpy.core.umath_tests import inner1d\n"
     ]
    }
   ],
   "source": [
    "%matplotlib inline \n",
    "\n",
    "import matplotlib.pyplot as plt \n",
    "\n",
    "import pandas as pd\n",
    "\n",
    "import sklearn.datasets\n",
    "from sklearn.model_selection import train_test_split as tts\n",
    "\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "\n",
    "from yellowbrick.classifier import ClassBalance\n",
    "from yellowbrick.classifier import ClassPredictionError\n",
    "from yellowbrick.classifier import ClassificationReport\n",
    "\n",
    "from ipywidgets import interact"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from yellowbrick.download import download_all\n",
    "\n",
    "## The path to the test data sets\n",
    "FIXTURES  = os.path.join(os.getcwd(), \"data\")\n",
    "\n",
    "## Dataset loading mechanisms\n",
    "datasets = {\n",
    "    \"occupancy\": os.path.join(FIXTURES, \"occupancy\", \"occupancy.csv\"),\n",
    "}\n",
    "\n",
    "\n",
    "def load_data(name, download=True):\n",
    "    \"\"\"\n",
    "    Loads and wrangles the passed in dataset by name.\n",
    "    If download is specified, this method will download any missing files.\n",
    "    \"\"\"\n",
    "\n",
    "    # Get the path from the datasets\n",
    "    path = datasets[name]\n",
    "\n",
    "    # Check if the data exists, otherwise download or raise\n",
    "    if not os.path.exists(path):\n",
    "        if download:\n",
    "            download_all()\n",
    "        else:\n",
    "            raise ValueError((\n",
    "                \"'{}' dataset has not been downloaded, \"\n",
    "                \"use the download.py module to fetch datasets\"\n",
    "            ).format(name))\n",
    "\n",
    "\n",
    "    # Return the data frame\n",
    "    return pd.read_csv(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ipykernel_launcher.py:9: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.\n",
      "  if __name__ == '__main__':\n",
      "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ipykernel_launcher.py:10: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.\n",
      "  # Remove the CWD from sys.path while we load stuff.\n"
     ]
    }
   ],
   "source": [
    "# Load the classification data set\n",
    "data = load_data(\"occupancy\")\n",
    "\n",
    "# Specify the features of interest and the classes of the target\n",
    "features = [\"temperature\", \"relative humidity\", \"light\", \"C02\", \"humidity\"]\n",
    "classes = [\"unoccupied\", \"occupied\"]\n",
    "\n",
    "# Extract the numpy arrays from the data frame\n",
    "X = data[features].as_matrix()\n",
    "y = data.occupancy.as_matrix()\n",
    "\n",
    "# Create the train and test data\n",
    "X_train, X_test, y_train, y_test = tts(X, y, test_size=0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a18a92ea143746cf86c15a1137e9bc58",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(Dropdown(description='model', options=('ClassBalance', 'ClassPredictionError', 'Classifi…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "models = {\n",
    "    cls.__name__: cls\n",
    "    for cls in (ClassBalance, ClassPredictionError, ClassificationReport)\n",
    "}\n",
    "\n",
    "@interact(model=list(models.keys()))\n",
    "\n",
    "def graph_classifers(model=\"ClassBalance\"):\n",
    "    _, axes = plt.subplots(ncols=1, figsize=(15,5))  \n",
    "        \n",
    "    viz = models[model](RandomForestClassifier(), classes=classes)\n",
    "    viz.fit(X_train, y_train)\n",
    "    viz.score(X_test, y_test)\n",
    "    viz.finalize()\n",
    "    \n",
    "    return axes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
